<template>
  <div :class="'his-box' + (type == 5 ? ' is-program' : '')">
    <p class="tit" v-if="type !== 5">提交记录</p>
    <ul v-if="newData && newData.length > 0">
      <li v-for="(item, index) in newData" :key="item.id">
        <span class="num">{{ newData.length - index }}{{ type === 5 ? '.' : '' }}</span>
        <div class="cont" v-if="type !== 5">
          <el-radio v-if="type === 1">{{ item.ans[0] }}</el-radio>
          <el-checkbox-group v-if="type === 2" class="q-cb-group" v-model="checkList">
            <el-checkbox v-for="(subItem, index) in item.ans" :key="index" @change="handleCheckboxChange">{{
              subItem
            }}</el-checkbox>
          </el-checkbox-group>
          <template v-if="type === 3">
            <div class="com-row" v-for="(subItem, index) in item.ans" :key="index">
              <span>{{ index + 1 }}</span
              >{{ subItem }}
            </div>
          </template>
          <el-input
            v-if="type === 4"
            type="textarea"
            v-model="item.ans[0]"
            resize="none"
            :autosize="{ minRows: 3 }"
            readonly
          >
          </el-input>
        </div>
        <span class="date">提交时间：{{ date(item.createTime) }}</span>
        <el-button
          type="text"
          icon="el-icon-delete-solid"
          class="btn-del"
          v-if="type !== 5"
          @click="handleDelete(item.id)"
          >删除</el-button
        >
        <div class="program-btns" v-if="type === 5">
          <el-button type="primary" plain class="btn-del" @click="handleDelete(item.id)">删除</el-button>
          <el-button class="btn-code" type="primary" @click="handleViewCode(item.ans)">查看代码</el-button>
        </div>
      </li>
    </ul>
    <el-empty description="暂无提交记录" v-if="newData.length < 1" :image-size="100"></el-empty>
  </div>
</template>

<script lang="ts">
import { Options, Vue } from 'vue-class-component';
import moment from 'moment';
import { PROGRAMURL } from '@/utils/constants';

@Options({
  props: {
    type: Number, // 1=单选；2=多选；3=填空；4=问答；5=编程
    historyData: Array,
    onDelete: Function,
    lang: Number,
  },
  computed: {
    date() {
      return (date: Date) => {
        return moment(date).format('YYYY/MM/DD HH:mm');
      };
    },
    newData() {
      const newData: ObjectT[] = [];
      if (this.historyData && this.historyData.length > 0) {
        this.historyData.map((item: ObjectT) => {
          newData.push({
            ...item,
            ans: item.ans.split('|'),
          });
        });
      }
      return newData;
    },
  },
})
export default class CommitHistory extends Vue {
  $confirm: any;
  type = 0;
  checkList = true;
  lang!: number;
  PROGRAMURL = PROGRAMURL;
  token = localStorage.getItem('token');
  onDelete!: (id: number) => {};
  // 处理复选框切换
  handleCheckboxChange() {
    this.checkList = true;
  }
  // 处理历史记录删除
  handleDelete(id: number) {
    this.$confirm('确认要删除该条提交记录吗？', '提示')
      .then(() => {
        if (this.onDelete) {
          this.onDelete(id);
        }
      })
      .catch(() => {
        // 取消
      });
  }

  // 获取编程题类型名称
  getLangName() {
    let langName = '';
    switch (this.lang) {
      case 2:
        langName = 'scratch';
        break;
      case 3:
        langName = 'python';
        break;
      default:
        break;
    }
    return langName;
  }
  // 查看代码
  handleViewCode(ans: string) {
    const langName = this.getLangName();
    const url = `${PROGRAMURL}${langName}/?token=${this.token}${ans ? `&codeUrl=${ans}` : ''}`;
    window.open(url, '_blank');
  }
}
</script>

<style scoped lang="scss">
@import './commitHistory.scss';
</style>
<style lang="scss">
.his-box {
  display: block;
  .q-cb-group {
    display: inline-block;
    vertical-align: middle;
  }
  .el-radio,
  .el-checkbox {
    display: block;
    &.is-checked {
      .el-radio__label,
      .el-checkbox__label {
        color: $text-color;
      }
    }
  }
  .el-radio__label,
  .el-checkbox__label,
  .el-radio__inner,
  .el-checkbox__inner {
    display: inline-block;
    vertical-align: middle;
  }
  .el-radio__label,
  .el-checkbox__label {
    font-size: 16px;
    color: $text-color;
    font-weight: normal;
    margin-top: 1px;
  }
  .el-radio__inner,
  .el-checkbox__inner {
    width: 16px;
    height: 16px;
  }
  .el-checkbox {
    display: block;
    margin: 15px 0;
  }
  .el-checkbox__inner {
    &:after {
      margin-top: 1px;
      margin-left: 1px;
    }
  }
  .el-input-group {
    display: table;
    margin: 10px 0;
    width: 290px;
  }
  .el-input-group__prepend {
    width: 40px;
    height: 36px;
    padding: 0;
    text-align: center;
    font-size: 17px;
    color: $text-color;
    background: transparent;
  }
  .el-input__inner {
    height: 38px;
    font-size: 13px;
    color: $text-color;
    background: #fff;
  }
  .el-textarea {
    width: 395px;
    .el-textarea__inner {
      background: $bg-color-purle;
      line-height: 30px;
      color: $text-black;
      font-size: 16px;
    }
  }
}
</style>
